#!/usr/bin/env python
# -*- coding: utf-8 -*-

from web.manager.oracle_manager import OracleManager


class BaostockBondIndexDao:
    """
    BaostockBondIndex的dao类
    """

    def __init__(self):
        self.db = OracleManager()

    def batch_insert_baostock_bond_index(self, param_list):
        """
        批量插入
        """

        self.db.connect()
        self.db.batch_insert("insert into bs_bond_index(date_, code_, open_price, highest_price, "
                                         "lowest_price, close_price, pre_close_price, volume, amount, "
                                         "change_percentage) "
                                         "values(:1, :2, :3, :4, :5, :6, :7, :8, :9, :10)", param_list)
        self.db.close()

    def find_date_and_close_price_by_code_between_date_order_by_date(self, code, start_date, end_date):
        """
        查询某个债券指数的收盘价，并按升序排列
        """

        self.db.connect()
        close_price_list = self.db.execute_query(
            "select t.date_, t.close_price from BS_BOND_INDEX t where t.code_='" + code + "' "
            "and t.date_ between to_date('" + start_date + "','yyyy-mm-dd') "
                                                           "and to_date('" + end_date + "','yyyy-mm-dd') "
                                                                                        "order by t.date_ asc")
        self.db.close()
        return close_price_list

    def find_date_by_code_between_date_order_by_date(self, code, start_date, end_date):
        """
        查询某个债券指数的日期，并按升序排列
        """

        self.db.connect()
        date_list = self.db.execute_query(
            "select t.date_ from BS_BOND_INDEX t where t.code_='" + code + "' "
            "and t.date_ between to_date('" + start_date + "','yyyy-mm-dd') "
                                                           "and to_date('" + end_date + "','yyyy-mm-dd') "
                                                                                        "order by t.date_ asc")
        self.db.close()
        return date_list
